Vue.component('time-check',
  {
    template: "<div><div id='segmentedControl' class='mui-segmented-control mui-segmented-control-inverted mui-segmented-control-primary' style='position: fixed;top:0;z-index: 999'>\
					<a class='mui-control-item' :class='{muiActive:actnum*1===0}' index='0'>按日</a>\
					<a class='mui-control-item' :class='{muiActive:actnum*1===1}' index='1'>按周</a>\
					<a class='mui-control-item' :class='{muiActive:actnum*1===2}' index='2'>按月</a>\
				</div>\
				<div class='time-choose' style='clear:both;margin-top: 0.5rem;height: 1.7rem'>\
					<div class='befor-month' @click='reduceTime()'  style='position: fixed;top:1rem;z-index: 999;top:0.8rem'>\
						<img src='../images/left.png' alt=''>\
						<span>上一{{actnum*1===0?'日':actnum*1===1?'周':'月'}}</span>\
					</div>\
					<div class='time-range' style='    padding-top:0.7rem'>\
					  <p> <strong style='color: black'>{{week2}}</strong></p> \
						<p v-show='actnum !==0' :class='{coinClass:type===2||type===3}' style='color: black' >{{new Date(fromdate).format('yyyy-MM-dd')}} 至 {{new Date(todate).format('yyyy-MM-dd')}} </p>\
						<p v-show='actnum ===0' :class='{coinClass:type===2||type===3}' style='color: black'>{{new Date(fromdate).format('yyyy-MM-dd')}}</p>\
					</div>\
					<div class='next-month' @click='increaseTime()'  style='position: fixed;z-index: 999;top:0.8rem' >\
						<span>下一{{actnum*1===0?'日':actnum*1===1?'周':'月'}}</span>\
						<img src='../images/right.png' alt=''>\
					</div></div>\
				</div>",
    data: function() {
      return {
        dateBase: 1,
        date: '',
        startTime: '',
        endTime: '',
        // isShow:1,   // 0 只显示当天
      }
    },
    props:{
      names:{
        default:function(){
          return {
            leftName:'',
            rightName:''
          }
        }
      },
      totalnum:{
        default:0
      },
      realnum:{
        default:0
      },
      leftdata:{
        default:0
      },
      rightdata:{
        default:0
      },
      actnum:{
        default:0
      },
      fromdate:{
        default:'--'
      },
      todate:{
        default:'--'
      },
      type:{
        default:1
      },
      dateType:{
      },
      top:{},
      leftunit:{
        default:''
      },  //单位
      rightunit:{
        default:''
      },  //单位



    },
    created: function() {
      this.initTime()
    },
    computed:{
      week:function(){
        var week=['星期天','星期一','星期二','星期三','星期四','星期五','星期六'];
        var time = new Date(this.fromdate).getDay();
        return week[time]
      },
      week2:function(){
        // var fullYear=(this.endTime+'').replace(/^(\d{4})(\d{2})(\d{2})$/, "$1");
        var data='';
        if(this.actnum ===1){
          var endTime=new Date(this.fromdate);
          var dayWeek=endTime.getDay();
          if(dayWeek ===0){dayWeek=7}
          var year=endTime.getFullYear();
          var firstDay = new Date(endTime.getFullYear(),0, 1);
          var dayOfWeek = firstDay.getDay();
          if (dayOfWeek ===0) {
            dayOfWeek=7;
          }
          var weekNum =parseInt( ((endTime.getTime()-firstDay.getTime())/(24*60*60*1000)+dayOfWeek-dayWeek)/7);
          data=year+'第'+ weekNum+'周'
        }else if(this.actnum === 0) {
          data=this.week
        }else {
          var endTime=new Date(this.todate);
          var year=endTime.getFullYear();
          var month=endTime.getMonth()+1;
          data=year+'年'+month+'月'
        }
        return data;
      },
      unitLeft:function(){
        return  this.leftdata !=='--' ? this.leftunit : ''
      },
      unitRight:function(){
        return  this.rightdata !=='--' ? this.rightunit : ''
      }
    },
    mounted:function(){
      var vm = this;
      mui('#segmentedControl').on('tap','.mui-control-item',function(){
        var index = this.getAttribute('index')*1;
        vm.$parent.dateType=index;
        vm.changeDatabase(index);
      })
    },
    methods: {
      initTime: function() {
        this.date = new Date();
        this.startTime=new Date(this.date).format('yyyy-MM-dd')
        this.endTime=new Date(this.date).format('yyyy-MM-dd')
        this.$parent.dateType=0;
      },
      changeDatabase: function(type) {
        var vm = this;
        this.$parent.actnum =  type*1
        switch(type) {
          case 0:
            this.date = new Date()
            this.startTime = new Date(this.date).format('yyyy-MM-dd')
            this.endTime = new Date(this.date).format('yyyy-MM-dd ')
            break;
          case 1:
            this.date = new Date();
            var weekday = this.date.getDay();//0:周日，1-6：周一到周六
            if(weekday===0){weekday=7};
            var startTime=new Date().getTime() - (24*60*60*1000)*(weekday-1);
            this.startTime=new Date(startTime).format('yyyy-MM-dd')
            this.endTime = new Date(this.date).format('yyyy-MM-dd')
            break;
          case 2:
            this.date = new Date()
            this.startTime = new Date(this.date).format('yyyy-MM-') + '01'
            this.endTime = new Date(this.date).format('yyyy-MM-dd')
            break;
        }
        this.$parent.toDate = this.endTime
        this.$parent.fromDate = this.startTime
      },
      //					      减小时间
      reduceTime: function() {
        switch(this.actnum) {
          case 0:
            this.date = this.date - 86400000
            this.startTime = new Date(this.date).format('yyyy-MM-dd')
            this.endTime = new Date(this.date).format('yyyy-MM-dd')
            break;
          case 1:
            var weekday = new Date(this.date).getDay();//0:周日，1-6：周一到周六
            if(weekday === 0) weekday = 7;
            this.date = this.date - weekday * 86400000
            this.startTime = new Date(this.date - 6 * 86400000).format('yyyy-MM-dd')
            this.endTime = new Date(this.date).format('yyyy-MM-dd')
            break;
          case 2:
            this.date = this.date - 86400000 * new Date(this.date).format('dd')
            this.startTime = new Date(this.date).format('yyyy-MM-') + '01'
            this.endTime = new Date(this.date).format('yyyy-MM-dd')
            break;
        }
        this.$parent.toDate = this.endTime
        this.$parent.fromDate  = this.startTime

      },
      //      增加时间
      increaseTime: function() {
        switch(this.actnum) {
          case 0:
            if(new Date(this.date + 86400000).format('yyyyMMdd')*1 === new Date().format('yyyyMMdd')*1) {
              this.date = new Date()
              this.startTime = new Date(this.date).format('yyyy-MM-dd')
              this.endTime = new Date(this.date).format('yyyy-MM-dd ')
            } else if(new Date(this.date + 86400000).format('yyyyMMdd')*1 < new Date().format('yyyyMMdd')*1) {
              this.date = this.date + 86400000
              this.startTime = new Date(this.date).format('yyyy-MM-dd')
              this.endTime = new Date(this.date).format('yyyy-MM-dd')
            }

            break;
          case 1:
            var weekday = new Date().getDay();//0:周日，1-6：周一到周六
            if(weekday === 0) weekday = 7;
            if(new Date(this.date+weekday*86400000).format('yyyyMMdd')*1  === new Date().format('yyyyMMdd')*1 ) {
              this.date = new Date();
              var startTime=new Date().getTime() - (86400000)*(weekday-1);
              this.startTime=new Date(startTime).format('yyyy-MM-dd')
              this.endTime = new Date(this.date).format('yyyy-MM-dd')
            } else if(new Date(this.date+86400000).format('yyyyMMdd')*1  < new Date().format('yyyyMMdd')*1 ) {
              this.startTime = new Date(this.date+86400000).format('yyyy-MM-dd')
              this.endTime = new Date(this.date+ 7 * 86400000).format('yyyy-MM-dd')
              this.date = this.date+ 7 * 86400000;
            }
            break;
          case 2:
            if(new Date(this.date).format('yyyy') < new Date().format('yyyy') || [new Date(this.date).format('yyyy') === new Date().format('yyyy') && (new Date(this.date).format('MM') * 1 + 1) < new Date().format('MM') * 1]) {
              var array = new Date(this.date).format('yyyy-MM').split('-')
              var nextMonthday = 32 - new Date(array[0] * 1, array[1] * 1, 32).getDate()
              this.date = this.date + 86400000 * nextMonthday
              this.startTime = new Date(this.date).format('yyyy-MM-') + '01'
              this.endTime = new Date(this.date).format('yyyy-MM-dd')
            }
            if(new Date(this.date).format('yyyy-MM') === new Date().format('yyyy-MM')) {
              this.date = new Date()
              this.startTime = new Date(this.date).format('yyyy-MM-') + '01'
              this.endTime = new Date().format('yyyy-MM-dd')
            }
            break;
        }
        this.$parent.toDate = this.endTime
        this.$parent.fromDate  = this.startTime
      }
    }
  })